package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinMemberService;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017/8/21
 * 描述：搜鸟私厨_用户相关controller
 */
@Controller
@RequestMapping("/sunday/small/member")
@Api(value = "member",description = "用户")
public class SundaySmallMemberController extends BaseController {
    @Autowired
    private SundayWeiXinMemberService memberService;

    @RequestMapping(value="/loginWithUnion", method= {RequestMethod.POST})
    @ApiOperation(value="用户通过unionID登录。手机号和密码。-1直接提示消息")
    public void loginWithUnion(HttpServletRequest request,
                      HttpServletResponse response,
                      @ApiParam(required=true, name="memberId", value="当前用户id")@RequestParam(value = "memberId", required = true)Long memberId,
                      @ApiParam(required=true, name="code", value="小程序登录code")@RequestParam(value = "code", required = true)String code,
                      @ApiParam(required=true, name="encryptedData", value="加密数据")@RequestParam(value = "encryptedData", required = true)String encryptedData,
                      @ApiParam(required=true, name="iv", value="iv")@RequestParam(value = "iv", required = true)String iv,
                      @ApiParam(required=false, name="mobile", value="手机号")@RequestParam(value = "mobile", required = false)String mobile,
                      @ApiParam(required=false, name="validateCode", value="验证码")@RequestParam(value = "validateCode", required = false)String validateCode
    ){
        try{
            SundayMember member=memberService.loginWithUnion(memberId,code,encryptedData,iv,mobile,validateCode);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code1=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code1=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/small/member/loginWithUnion----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code1,msg);
        }
    }

    @RequestMapping(value="/loginWithCode", method= {RequestMethod.POST})
    @ApiOperation(value="用户登陆（只获取openId）。手机号和密码。-1直接提示消息")
    public void login(HttpServletRequest request,
                      HttpServletResponse response,
                      @ApiParam(required=true, name="code", value="小程序登录code")@RequestParam(value = "code", required = true)String code
    ){
        try{
            SundayMember member=memberService.loginWithCode(code);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code1=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code1=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/small/member/loginWithCode----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code1,msg);
        }
    }

    @RequestMapping(value="/findOne", method= {RequestMethod.POST})
    @ApiOperation(value="查找单个。-1直接提示消息")
    public void findOne(HttpServletRequest request,
                      HttpServletResponse response,
                      @ApiParam(required=false, name="memberId", value="用户id")@RequestParam(value = "memberId", required = true)Long memberId

    ){
        try{
            SundayMember member=memberService.getById(memberId);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code1=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code1=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/small/member/findOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code1,msg);
        }
    }

    /**
     * 统一修改接口
     * @param request
     * @param response
     * @param nickName
     * @param mobile
     * @param sex
     */
    @RequestMapping(value="/updateMember", method= {RequestMethod.POST})
    public void updateMember(HttpServletRequest request,
                     HttpServletResponse response,
                      @RequestParam(value = "memberId", required = true)Long memberId,
                     @RequestParam(value = "nickName", required = false)String nickName,
                     @RequestParam(value = "image", required = false)String image,
                     @RequestParam(value = "mobile", required = false)String mobile,
                     @RequestParam(value = "sex", required = false)String sex,
                     @RequestParam(value = "validateCode", required = false)String validateCode
    ){
        try{
            SundayMember member=memberService.updateMember(memberId,nickName,image,sex,mobile,validateCode);
            ResponseMessage.success(member);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/small/member/updateMember----e="+e.getMessage());
                e.printStackTrace();
            }
            ResponseMessage.error(code,msg);

        }
    }




}
